const fs = require('fs');
const { resolve, dirname } = require('path');
const { load, dump } = require('js-yaml');


function  read(pth){
    const absPath = resolve(__dirname,'../',pth)
    const content =  fs.readFileSync(absPath , { encoding: 'utf-8' })
    return content
}
function write(pth,content){
    const absPath = resolve(__dirname,'../',pth)
    const dir = dirname(absPath)
    if(!fs.existsSync(dir)){
        fs.mkdirSync(dir,{recursive:true})
    }
    fs.writeFileSync(absPath,content,{encoding:'utf-8'})
}
module.exports = {
    read,
    readYaml(pth){
        const content = read(pth)
        return load(content)
    },
    write,
    writeYaml(pth,obj){
        write(pth,dump(obj))
    },
    writeJson(pth,obj){
        write(pth,JSON.stringify(obj,null,4))
    }
}